//------------------------------------------------------------------------------
//  <copyright file="MockFRIConnection.cpp" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
//  </copyright>
//------------------------------------------------------------------------------

#include "stdafx.h"
#include "MockFRIConnection.h"

/// <summary> Initializes a new instance of the <c>MockFRIConnection</c> class. </summary>
MockFRIConnection::MockFRIConnection() 
{
	// working with the feedback which is just the latter portion of the registers used by
	// the state machine.  Use same format for simplicity
	this->pLbrFeedback = new ControllerStateMachineRegisters<double>(
		KukaLBR4PlusFeedbackRegisters::NumberOfFeedbackRegisters,
		(unsigned int *)&(KukaLBR4PlusRegisterSizes[KukaLBR4PlusWorkingRegisters::NumberOfWorkingRegisters]));

}

/// <summary> dtor </summary>
MockFRIConnection::~MockFRIConnection()
{
	delete this->pLbrFeedback;
}

/// <summary> Shut down the FRI server. </summary>
/// <returns> Status code.  0 on no error. </returns>
int MockFRIConnection::CloseFRI()
{
	// nothing to do here for mock
	return 0;
}

/// <summary> Wait for an FRI client to connect. </summary>
/// <returns> Status code.  0 on no error. </returns>
int MockFRIConnection::WaitOnFRIConnect()
{
	// nothing to do here for mock
	return 0;
}

/// <summary> Wait on a new FRI request. </summary>
/// <returns> State of the LBR as indicated by the next FRI request. </returns>
int MockFRIConnection::GetFRIRequest(double **ppFeedbackRegisters)
{
	for(unsigned int i = 0; i<KukaLBR4PlusFeedbackRegisters::NumberOfFeedbackRegisters; ++i)
	{
		memcpy(
			ppFeedbackRegisters[i],
			this->pLbrFeedback->GetRegister(i),
			this->pLbrFeedback->GetRegisterSizeInBytes(i));
	}

	return 0;
}

/// <summary> Gets the quality of the FRI connection </summary>
/// <returns> The quality of the FRI connection. </returns>
FRIQuality MockFRIConnection::GetFRIQuality()
{
	return FRIQuality::FRIQualityPerfect;
}

/// <summary> Send a response to the FRI request, but take no control actions in it. </summary>
/// <returns> Status code.  0 on no error. </returns>
int MockFRIConnection::DoNothing()
{
	// nothing to do here for mock
	return 0;
}

/// <summary> Execute joint position control. </summary>
/// <param name="jointPosition"> Array of joint positions. </param>
/// <returns> Status code.  0 on no error. </returns>
int MockFRIConnection::DoPositionControl(double *jointPosition)
{
	memcpy_s(
		this->pLbrFeedback->GetRegister(KukaLBR4PlusFeedbackRegisters::CommandedJointPositionRegister),
		this->pLbrFeedback->GetRegisterSizeInBytes(KukaLBR4PlusFeedbackRegisters::CommandedJointPositionRegister),
		jointPosition,
		NUM_JOINTS*sizeof(double));

	memcpy_s(
		this->pLbrFeedback->GetRegister(KukaLBR4PlusFeedbackRegisters::MeasuredJointPositionRegister),
		this->pLbrFeedback->GetRegisterSizeInBytes(KukaLBR4PlusFeedbackRegisters::MeasuredJointPositionRegister),
		jointPosition,
		NUM_JOINTS*sizeof(double));

	return 0;
}

/// <summary> Execute joint impedance control. </summary>
/// <param name="jointPosition"> Array of joint positions. </param>
/// <param name="jointStiffness"> Array of joint stiffness values. </param>
/// <param name="jointDamping"> Array of joint damping values. </param>
/// <param name="jointAdditionalTorque"> Array of addition joint torque values. </param>
/// <returns> Status code.  0 on no error. </returns>
int MockFRIConnection::DoJointImpedanceControl(
	double *jointPosition,
	double *jointStiffness,
	double *jointDamping,
	double *jointAdditionalTorque)
{
	memcpy_s(
		this->pLbrFeedback->GetRegister(KukaLBR4PlusFeedbackRegisters::CommandedJointPositionRegister),
		this->pLbrFeedback->GetRegisterSizeInBytes(KukaLBR4PlusFeedbackRegisters::CommandedJointPositionRegister),
		jointPosition,
		NUM_JOINTS*sizeof(double));
	memcpy_s(
		this->pLbrFeedback->GetRegister(KukaLBR4PlusFeedbackRegisters::MeasuredJointPositionRegister),
		this->pLbrFeedback->GetRegisterSizeInBytes(KukaLBR4PlusFeedbackRegisters::MeasuredJointPositionRegister),
		jointPosition,
		NUM_JOINTS*sizeof(double));
	memcpy_s(
		this->pLbrFeedback->GetRegister(KukaLBR4PlusFeedbackRegisters::MeasuredJointTorqueRegister),
		this->pLbrFeedback->GetRegisterSizeInBytes(KukaLBR4PlusFeedbackRegisters::MeasuredJointTorqueRegister),
		jointAdditionalTorque,
		NUM_JOINTS*sizeof(double));

	return 0;
}

/// <summary> Execute Cartesian impedance control. </summary>
/// <param name="cartesianPosition"> Array indicating desired cartesian position relative to base frame. </param>
/// <param name="cartesianStiffness"> Array of Cartesian stiffness values. </param>
/// <param name="cartesianDamping"> Array of Cartesian damping values. </param>
/// <param name="additionalTCPForceTorque"> Array of additional TCP force torque values. </param>
int MockFRIConnection::DoCartesianImpedanceControl(
	double *cartesianPosition,
	double *cartesianStiffness,
	double *cartesianDamping,
	double *additionalTCPForceTorque)
{
	memcpy_s(
		this->pLbrFeedback->GetRegister(KukaLBR4PlusFeedbackRegisters::CommandedCartesianPositionRegister),
		this->pLbrFeedback->GetRegisterSizeInBytes(KukaLBR4PlusFeedbackRegisters::CommandedCartesianPositionRegister),
		cartesianPosition,
		CARTESIAN_FRAME_LENGTH*sizeof(double));

	memcpy_s(
		this->pLbrFeedback->GetRegister(KukaLBR4PlusFeedbackRegisters::MeasuredCartesianPositionRegister),
		this->pLbrFeedback->GetRegisterSizeInBytes(KukaLBR4PlusFeedbackRegisters::MeasuredCartesianPositionRegister),
		cartesianPosition,
		CARTESIAN_FRAME_LENGTH*sizeof(double));

	memcpy_s(
		this->pLbrFeedback->GetRegister(KukaLBR4PlusFeedbackRegisters::EstimatedTCPForceTorqueRegister),
		this->pLbrFeedback->GetRegisterSizeInBytes(KukaLBR4PlusFeedbackRegisters::EstimatedTCPForceTorqueRegister),
		additionalTCPForceTorque,
		CARTESIAN_DIM*sizeof(double));
	return 0;
}

/// <summary> Execute Cartesian impedance control by passing position goals to KRL script. </summary>
/// <param name="pCartesianPosition"> Array indicating desired cartesian position of TCP (tool control point) relative to base frame. </param>
/// <param name="sequenceNumber"> Used to identify when a new position command is ready to be executed. </param>
/// <returns> 0 on success </returns>
int MockFRIConnection::DoKrlCartesianImpedanceControl(double *pCartesianPosition, int sequenceNumber)
{
	memcpy_s(
		this->pLbrFeedback->GetRegister(KukaLBR4PlusFeedbackRegisters::CommandedCartesianPositionRegister),
		this->pLbrFeedback->GetRegisterSizeInBytes(KukaLBR4PlusFeedbackRegisters::CommandedCartesianPositionRegister),
		pCartesianPosition,
		CARTESIAN_FRAME_LENGTH*sizeof(double));

	memcpy_s(
		this->pLbrFeedback->GetRegister(KukaLBR4PlusFeedbackRegisters::MeasuredCartesianPositionRegister),
		this->pLbrFeedback->GetRegisterSizeInBytes(KukaLBR4PlusFeedbackRegisters::MeasuredCartesianPositionRegister),
		pCartesianPosition,
		CARTESIAN_FRAME_LENGTH*sizeof(double));

	return 0;
}
